package com.sky.service.impl.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result<PageResult> getCategoryByPage(CategoryPageQueryDTO dto) {
        Page<Category> page = new Page<>(dto.getPage(), dto.getPageSize());

        List<Category> categoryByPage = categoryMapper.getCategoryByPage(dto);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryByPage);

        PageResult result = new PageResult(pageInfo.getTotal(), pageInfo.getList());

        return Result.success(result);
    }

    @Override
    public Result addCategory(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);

        // 新发布菜品默认禁用
        category.setStatus(StatusConstant.DISABLE);

        categoryMapper.insert(category);

        return Result.success();

    }

    @Override
    public Result updateCategory(Category category) {
//        List<DishVO> dishListAllInfoByCategoryId = dishMapper.getDishListAllInfoByCategoryId(category.getId());
//        if (dishListAllInfoByCategoryId == null || dishListAllInfoByCategoryId.isEmpty()) throw new BaseException(MessageConstant.ENABLE_DISH_ON_CATEGORY_NULL);

        if(categoryMapper.updateCategoryById(category) == 0) throw new BaseException("更新失败");

        return Result.success();

    }

    @Override
    public Result deleteCategoryById(Long id) {
        int count = dishMapper.findDishCountByCategoryId(id);
         // 如果当前分类关联了菜品
        if (count > 0) throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);

        count = setmealMapper.findSetmealCountByCategoryId(id);

        // 如果当前分类关联的套餐
        if (count > 0) throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);

        // 如果删除失败
        if(categoryMapper.deleteCategoryById(id) == 0) throw new BaseException("删除失败");

        return Result.success();
    }

    @Override
    public Result<List<Category>> getCategoryListByType(Long type) {
        List<Category> categoryList = categoryMapper.getCategoryListByType(type);
        if (categoryList == null || categoryList.isEmpty()) throw new BaseException("用户端查询分类失败");

        return Result.success(categoryList);
    }


}
